package com.green.service.manage.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.green.common.page.Pagination;
import com.green.dao.manage.PersonDao;
import com.green.model.manage.Area;
import com.green.model.manage.AreaPermission;
import com.green.model.manage.Org;
import com.green.model.manage.Person;
import com.green.model.manage.enums.OrgEnum;
import com.green.model.manage.enums.PositionEnum;
import com.green.service.common.ServiceContext;
import com.green.service.common.impl.BaseServiceImpl;
import com.green.service.manage.AreaPermissionService;
import com.green.service.manage.AreaService;
import com.green.service.manage.OrgService;
import com.green.service.manage.PersonService;

/**
 * 人员
 * 
 */
@Component("personService")
public class PersonServiceImpl extends BaseServiceImpl implements PersonService {
	@Autowired
	private PersonDao personDao;
	@Autowired
	private AreaPermissionService areaPermissionService;
	@Autowired
	private AreaService areaService;
	@Autowired
	private OrgService orgService;

	@Override
	public List<Person> getLoginUser(Map<String, Object> map) {
		return this.personDao.getLoginUser(map);
	}
	
	public Person getBaseInfoPersonById(String id) {
		return personDao.getPersonById(id);
	}

	@Override
	public Person getPersonById(String id) {
		Person person = personDao.getPersonById(id);
		if(person == null){
			return null;
		}

		String cityName = "";
		String areaName = "";
		String plateName = "";
	    String cityIds= "";	//拼接城市Ids
	    String areaIds= "";	//拼接区域Ids
	    String plateIds= "";	//拼接板块Ids
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("personId", id);
		List<AreaPermission> apList = this.areaPermissionService
				.getAreaPermissionList(param);
		for (AreaPermission areaPermission : apList) {
			if (areaPermission.getArea().getLevel() == 5) {
				if (plateName == "") {
					plateName = areaPermission.getArea().getName();
					plateIds =areaPermission.getArea().getId();
				} else {
					plateName = plateName + ","
							+ areaPermission.getArea().getName();
					plateIds = plateIds + ","
					+ areaPermission.getArea().getId();
				}
				// 根据模块获取区
				Area area = areaService.getAreaById(areaPermission.getArea()
						.getParent().getId());
				if (!areaName.contains(area.getName())) {
					if (areaName == "") {
						areaName = area.getName();
						areaIds=area.getId();
					} else {
						areaName = areaName + "," + area.getName();
						areaIds=areaIds+","+area.getId();
					}
				}
				// 根据区获取市
				Area city = areaService.getAreaById(area.getParent().getId());
				if (!cityName.contains(area.getName())) {
					if (cityName == "") {
						cityName = city.getName();
						cityIds=city.getId();
					} else {
						if (!cityName.contains(city.getName())) {
							cityName = cityName + "," + city.getName();
							cityIds=cityIds+","+city.getId();
						}
					}
				}
			} else if(areaPermission.getArea().getLevel() == 3) {
				if (areaName == "") {
					areaName = areaPermission.getArea().getName();
					areaIds=areaPermission.getArea().getId();
				} else {
					areaName = areaName + ","
							+ areaPermission.getArea().getName();
					areaIds= areaIds + ","
					+ areaPermission.getArea().getId();
				}
				// 根据区获取市
				Area area = areaService.getAreaById(areaPermission.getArea()
						.getParent().getId());
				if (!cityName.contains(area.getName())) {
					if (cityName == "") {
						cityName = area.getName();
						cityIds=area.getId();
					} else {
						if (!cityName.contains(area.getName())) {
							cityIds=cityIds+","+area.getId();
						}
					}
				}
			}else{				
				// 根据区获取市
				Area area = areaService.getAreaById(areaPermission.getArea().getId());
				if (!cityName.contains(area.getName())) {
					if (cityName == "") {
						cityName = area.getName();
						cityIds=area.getId();
					} else {
						if (!cityName.contains(area.getName())) {
							cityName = cityName+","+area.getName();
							cityIds=cityIds+","+area.getId();
						}
					}
				}
			}
		}
		person.setCityName(cityName);
		person.setAreaName(areaName);
		person.setPlateName(plateName);
		
		person.setCityIds(cityIds);
		person.setAreaIds(areaIds);
		person.setPlateIds(plateIds);
		return person;

	}

	@Override
	public Person getPersonById(String id, boolean adminFlag) {
		return null;
	}

	@Override
	public Pagination<Person> getPersonPagination(Pagination<Person> pag,
			Map<String, Object> param) {
		pag=personDao.getPersonPagination(pag, param);
		List<Person> list=pag.getItems();
		if(null!=pag&&list.size()>0){
			List<Person> personList=new ArrayList<Person>();
			
			for(int i=0;i<list.size();i++){
				if(!list.get(i).getNumber().contains("admin")){
				personList.add(getPersonById(list.get(i).getId()));
				}
			}
			pag.setItems(personList);
		}
		
		return pag;
	}

	@Override
	public int insertPerson(Person ct) {
		return personDao.insertPerson(ct);
	}

	@Override
	public int updatePerson(Person ct) {
		return personDao.updatePerson(ct);
	}

	@Override
	public int deletePerson(Map<String, Object> param) {
		return personDao.deletePerson(param);
	}

	@Override
	public List<Person> getPersonList(Map<String, Object> param) {
		return this.personDao.getPersonList(param);
	}

	public List<Person> queryFuzzySearchByName(String name,String city) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("name", name);
		param.put("querySaler", "true");
		param.put("cityname", city);
		param.put("limitData", "true");
		return personDao.queryPersons(param);
	}
	public List<Person> queryFuzzySearchJoinPersionByName(String name,String orgids) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("name", name);
		//param.put("querySaler", "true");
		param.put("limitData", "true");
		param.put("orgids", orgids);
		if(StringUtils.isNotBlank(orgids))
			return personDao.queryPersons(param);
		return new ArrayList<Person>(0);
	}
	@Override
	public Person getCityManager(String personId) {
		Person p = personDao.getPersonById(personId);
		Org companyOrg = getTypeOfCompanyOrg(p.getOrg().getId());
		if(companyOrg==null){
		    return null;
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("orgId", companyOrg.getId());
		List<Person> persons = personDao.queryCitymanager(param);
		if(persons.size()>0)
			return persons.get(0);
		return null;
	}

	/**
	 * 由下而上查找公司所在的组织节点
	 * 
	 * @param id
	 * @return
	 */
	private Org getTypeOfCompanyOrg(String id) {
		Org org = orgService.getOrgById(id);
		if (OrgEnum.COMPANY.equals(org.getOrgType()) || OrgEnum.BLOC.equals(org.getOrgType()) ) {
			return org;
		} else{
			return getTypeOfCompanyOrg(org.getParent().getId());
		}
	}

	@Override
	public Person queryPersonByCity(String cityName, PositionEnum position) {

		return personDao.queryPersonByCity(cityName, position);
	}

	@Override
	public Pagination<Person> getAreaPersonPagination(Pagination<Person> pag,
			Map<String, Object> param) {
		return personDao.getAreaPersonPagination(pag, param);
	}

	@Override
	public Person getCityManager() {
		Map<String,Object> param = new HashMap<String, Object>();
        param.put("orgId", ServiceContext.getLoginPerson().getOrg().getId());
        return personDao.queryCitymanager(param).get(0);
	}

	@Override
	public Person getPersonParentOrgId(String personId) {
		return personDao.getPersonParentOrgId(personId);
	}

	@Override
	public Person getPersonParentPersonId(String personId) {
		return personDao.getPersonParentPersonId(personId);
	}

}
